/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.explorer.ftypes;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.urchr.web.db.DBSession;
import org.webos.java.base.ApplicationType;
import org.webos.java.base.db.apps.DBApplication;
import org.webos.java.base.fs.FileDescriptor;
import org.webos.java.base.fs.FileException;
import org.webos.java.base.fs.FileProvider;
import org.webos.java.base.fs.FileType;
import org.webos.java.base.ApplicationManager;
import org.webos.java.root.Log;

/**
 *
 * @author Kostya
 */
public class ApplicationFileProvider implements FileProvider{

    private ApplicationFileDescriptor root = getDefault();

    public ApplicationFileProvider() {
        root.setName("Applications");
        root.setId("applications folder");
        root.setIcon("explorer_icn explorer_icn_apps");
    }

    public FileDescriptor[] listFiles(FileDescriptor aDescriptor) throws FileException {
        Set<String> apps = ApplicationManager.getApplications();
        DBSession dbs = new DBSession();
        try {
            List<ApplicationFileDescriptor> result = new ArrayList<ApplicationFileDescriptor>();
            for (String code : apps) {
                DBApplication dba = (DBApplication) dbs.createQuery("from DBApplication where code=:code").
                        setString("code", code).uniqueResult();
                if(dba!=null && !dba.isSystemType()){
                    ApplicationFileDescriptor app = getDefault();
                    app.setApplicationCode(code);
                    app.setChildrenAllowed(false);
                    app.setParentFile(root);
                    app.setName(dba.getName());
                    app.setId("app"+dba.getId());
                    app.setType("application/link");
                    ApplicationType at = ApplicationManager.getApplicationInfo(code);
                    if(at!=null && !"".equals(at.icon())){
                        app.setIcon(at.icon());
                    }
                    result.add(app);
                }
            }
            return result.toArray(new ApplicationFileDescriptor[0]);
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        } finally {
            dbs.close();
        }
        return new ApplicationFileDescriptor[0];
    }

    public InputStream getInputStream(FileDescriptor aDescriptor) throws FileException {
        return null;
    }

    public OutputStream getOutputStream(FileDescriptor aDescriptor) throws FileException {
        return null;
    }

    public boolean removeFile(FileDescriptor aDescriptor, boolean aSafe) throws FileException {
        return false;
    }

    public FileDescriptor getRootFile() {
        return root;
    }

    private ApplicationFileDescriptor getDefault(){
        ApplicationFileDescriptor descriptor = new ApplicationFileDescriptor();
        descriptor.setEditable(false);
        descriptor.setProvider(this);
        descriptor.setRemovable(false);
        descriptor.setAllowedFileTypes(new FileType[0]);
        return descriptor;
    }

    public void createFolder(FileDescriptor aDescriptor) throws FileException {
        throw new FileException("Can't create folder here");
    }

    public void renameFile(FileDescriptor aDescriptor) throws FileException {
        throw new FileException("Can't rename application");
    }

    public RandomAccessFile getRandomAccessFile(FileDescriptor aDescriptor, String aAccess) throws FileException {
        throw new FileException("Unsupported operation");
    }

}
